using FluentValidation;
using WeatherSystem.API.Models;

namespace WeatherSystem.API.Validators
{
    /// <summary>
    /// 登录请求验证器
    /// </summary>
    public class LoginRequestValidator : AbstractValidator<LoginRequest>
    {
        public LoginRequestValidator()
        {
            RuleFor(x => x.Username)
                .NotEmpty().WithMessage("用户名不能为空")
                .Length(3, 50).WithMessage("用户名长度必须在3-50个字符之间")
                .Matches(@"^[a-zA-Z0-9_]+$").WithMessage("用户名只能包含字母、数字和下划线");

            RuleFor(x => x.Password)
                .NotEmpty().WithMessage("密码不能为空")
                .MinimumLength(6).WithMessage("密码长度至少6个字符");
        }
    }

    /// <summary>
    /// 注册请求验证器
    /// </summary>
    public class RegisterRequestValidator : AbstractValidator<RegisterRequest>
    {
        public RegisterRequestValidator()
        {
            RuleFor(x => x.Username)
                .NotEmpty().WithMessage("用户名不能为空")
                .Length(3, 50).WithMessage("用户名长度必须在3-50个字符之间")
                .Matches(@"^[a-zA-Z0-9_]+$").WithMessage("用户名只能包含字母、数字和下划线");

            RuleFor(x => x.Email)
                .NotEmpty().WithMessage("邮箱不能为空")
                .EmailAddress().WithMessage("邮箱格式不正确")
                .MaximumLength(100).WithMessage("邮箱长度不能超过100个字符");

            RuleFor(x => x.Password)
                .NotEmpty().WithMessage("密码不能为空")
                .MinimumLength(8).WithMessage("密码长度至少8个字符")
                .Matches(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]")
                .WithMessage("密码必须包含大小写字母、数字和特殊字符");

            RuleFor(x => x.ConfirmPassword)
                .NotEmpty().WithMessage("确认密码不能为空")
                .Equal(x => x.Password).WithMessage("确认密码与密码不匹配");
        }
    }

    /// <summary>
    /// 更改密码请求验证器
    /// </summary>
    public class ChangePasswordRequestValidator : AbstractValidator<ChangePasswordRequest>
    {
        public ChangePasswordRequestValidator()
        {
            RuleFor(x => x.CurrentPassword)
                .NotEmpty().WithMessage("当前密码不能为空");

            RuleFor(x => x.NewPassword)
                .NotEmpty().WithMessage("新密码不能为空")
                .MinimumLength(8).WithMessage("新密码长度至少8个字符")
                .Matches(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]")
                .WithMessage("新密码必须包含大小写字母、数字和特殊字符")
                .NotEqual(x => x.CurrentPassword).WithMessage("新密码不能与当前密码相同");

            RuleFor(x => x.ConfirmNewPassword)
                .NotEmpty().WithMessage("确认新密码不能为空")
                .Equal(x => x.NewPassword).WithMessage("确认新密码与新密码不匹配");
        }
    }

    /// <summary>
    /// 刷新令牌请求验证器
    /// </summary>
    public class RefreshTokenRequestValidator : AbstractValidator<RefreshTokenRequest>
    {
        public RefreshTokenRequestValidator()
        {
            RuleFor(x => x.RefreshToken)
                .NotEmpty().WithMessage("刷新令牌不能为空")
                .Length(32, 512).WithMessage("刷新令牌格式不正确");
        }
    }
}